#@layout()

#define main()
<div class="jfa-header-box" id="jfa-header-box">
	<div class="jfa-crumbs" id="jfa-crumbs">
		账户管理
	</div>
	#include("/_view/common/_header_right.html")
</div>
<div class="jfa-content-box" id="jfa-content-box">
	<div class="jfa-content" id="jfa-content">
	    <div class="margin-top-30">
			<form class="form-horizontal actionPanel" action="#" method="get">
		        <div class="row">
		             <div class="form-group col-lg-4">
		                 <label class="col-sm-3 control-label">姓名</label>
		                 <div class="col-sm-9">
		                     <input type="text" id="form-realName" class="form-control" name="realName" value="">
		                 </div>
		             </div>
		             <div class="form-group col-lg-4">
		                 <label class="col-sm-3 control-label">账户</label>
		                 <div class="col-sm-9">
		                     <input type="text" id="form-userName" class="form-control" name="userName" value="">
		                 </div>
		             </div>
		             <div class="form-group col-lg-4">
                         <div class="col-sm-12">
                             <button type="button" class="btn btn-primary" onclick="searchAccount()">查询</button>
                             <button type="reset" class="btn btn-default">重置</button>
                         </div>
                     </div>
		         </div>
		    </form>
		</div>
		

		<div id="toolbar" class="btn-group">
	        <a class="btn btn-primary btn-sm" href="#(ctx)/admin/account/add">
				<i class="fa fa-plus"></i>
				创建账户
			</a>
			
	    </div>
		<table id="account">
			
		</table>
	</div>
</div>
<script type="text/javascript">
var datagrid = function () {
 	var contentHeight = $("#jfa-content").height();
 	var datagridHeight=contentHeight-80;
    var _datagrid = new Object();
    _datagrid.init = function () {
        $('#account').bootstrapTable({
            url: '#(ctx)/admin/account/list',         
            method: 'get',
            toolbar: '#toolbar',
            striped: true,
            cache: false,
            pagination: true,
            sortable: false,                    
            sortOrder: "asc",  
            queryParams: _datagrid.queryParams,
            sidePagination: "server",
            pageNumber:1,
            toolbarAlign:"right",
            pageSize: 10,         
            pageList: [10, 20, 30, 40,50],
            search: false,
            strictSearch: true,
            showColumns: false,
            showRefresh: false,             
            minimumCountColumns: 2,
            clickToSelect: true,               
           	height: datagridHeight,
            uniqueId: "id",
            showToggle:false,
            cardView: false,
            detailView: false,
            columns: [
                  {
                    field: 'realName',  title: '姓名'
                },  {
                    field: 'userName',  title: '账号'
                }, {
                    field: 'employeeNumber',  title: '工号'
                }, {
                    field: 'createAt',  title: '创建时间'
                },{
                    title: '锁定',formatter:lockFormatter
                }, {
                    title: '操作',formatter:operaterFormatter
                }
            ],
            onLoadSuccess:function(){
            	initMagicInput(prepareAction);
            }
        });
    };

    //得到查询的参数
    _datagrid.queryParams = function (params) {
        var data = { 
            pageSize: params.limit,
            offset: params.offset,
            'userName': $("#form-userName").val(),
            'realName': $("#form-realName").val(),
        };
        return data;
    };
    return _datagrid;
};

function operaterFormatter(value, row, index){
	return "<a href='#(ctx)/admin/account/edit?id="+row.id+"'>"+
	"<i class='fa fa-pencil' title='修改'></i>"+
	" </a> &nbsp"+
	"<a data-pjax href=\"#(ctx)/admin/account/assignRoles?id="+row.id+"\">"+
	"	<i class='fa fa-user' title='分配角色'></i>"+
	"</a> &nbsp"+
	"<a  href='#' onclick='deleteAccount(\""+row.id+"\")'>"+
	"	<i class='fa fa-trash' title='删除'></i>"+
	"</a>"
}

function lockFormatter(value, row, index){
	var result="<input data-id='"+row.id+"'";
	if (row.status==0){
		result+=" checked ";
	}
	 
	result+="type='checkbox' class='mgc-switch mgc-tiny'>";	  

	return result;
}

var accountDatagrid = new datagrid();
accountDatagrid.init();


function searchAccount(){
	$('#account').bootstrapTable("refresh",{silent: true});
    layer.msg('操作成功');
}

function deleteAccount(id){
	if(id=="1"){
		layer.msg('不能删除超级管理员');
		return ;
	}
	layer.confirm("确认删除该账号？", {
		icon: 0
		, title:'' 
		, shade: 0.4
		, offset: "139px"
	}, function(index) { 
		$.ajax({
			url:"#(ctx)/admin/account/delete",
			data:{"id":id},
			dataType:"json",
			success:function(ret){
				$('#account').bootstrapTable("refresh");
			    layer.msg(ret.msg);
			    layer.close(index);   
			},
			error:function(){
				layer.close(index);   
			}
		});                           
		                       
	});
}


function prepareAction($this, state) {
	return {
		url: state ? "#(ctx)/admin/account/lock" : "#(ctx)/admin/account/unlock",
		data : {
			id: $this.attr("data-id")
		}
	}
}

</script>
#end
